package com.press.controllers.admin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.press.bean.UserBean;
import com.press.services.UserService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by haocongshun on 2016/10/14.
 */
@RequestMapping("/userManager")
@Controller("adminUser")
public class UserController {
    private static Logger log = Logger.getLogger(UserController.class);
    @Resource(name = "userService")
    private UserService userService;

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    @RequestMapping("/getAllUser")
    public void getAllUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<UserBean> listUser = userService.getUserInfo();
        request.setAttribute("users",listUser);
        log.info("用户数量："+listUser.size());
        request.getRequestDispatcher("/WEB-INF/admin/user/allUser.jsp").forward(request,response);
    }
    @RequestMapping("/addUser")
    private void addUser(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
        UserBean newUser = new UserBean();
        newUser.setUser_username(request.getParameter("username"));
        newUser.setUser_nickname(request.getParameter("nickname"));
        newUser.setUser_email(request.getParameter("email"));
        newUser.setUser_phone(request.getParameter("phone"));
        newUser.setUser_password(request.getParameter("password"));
        newUser.setUser_id(userService.getUserPk());
        CommonsMultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        if(resolver.isMultipart(request)){
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
            MultipartFile multiFile = multiRequest.getFile("avatar");
            if(!multiFile.isEmpty()){
                String filename = multiFile.getOriginalFilename();
                filename = "avatar_"+newUser.getUser_id()+filename.substring(filename.lastIndexOf("."));
                String path = request.getServletContext().getRealPath("/") + "/static/file/avatar/";
                File file = new File(path,filename);
                if(!file.exists()){
                    file.createNewFile();
                }
                multiFile.transferTo(file);
                newUser.setUser_avatar("/static/file/avatar/" + filename);
            }
        }
        boolean msg = userService.addUser(newUser);
        log.info("用户添加是否成功？"+msg);
        if(msg){
            response.sendRedirect(request.getContextPath()+"/userManager/getAllUser.press");
        }else {
            request.setAttribute("Msg","<h3 style='font-weight: bold;color:red;'>用户添加失败！</h3>");
            request.getRequestDispatcher("/WEB-INF/admin/user/addUser.jsp").forward(request,response);
        }

    }
    @RequestMapping("/checkUser")
    private void checkUser(HttpServletRequest request,HttpServletResponse response) throws IOException {
        JSONObject json = new JSONObject();
        boolean msg = userService.checkUser(request.getParameter("username"));
        json.put("Msg",msg);
        log.info("用户名是否已经使用过？"+msg);
        response.getWriter().print(json.toJSONString());
    }
    @RequestMapping("/freezeUser")
    private void freezeUser(HttpServletRequest request,HttpServletResponse response) throws IOException {
        boolean msg = userService.freezeUser(request.getParameter("user_id"));
        JSONObject json = new JSONObject();
        json.put("Msg",msg);
        log.info("冻结成功？"+json.toJSONString());
        response.getWriter().print(json.toJSONString());
    }
    @RequestMapping("/unfreezeUser")
    private void unfreezeUser(HttpServletRequest request,HttpServletResponse response) throws IOException {
        boolean msg = userService.unfreezeUser(request.getParameter("user_id"));
        JSONObject json = new JSONObject();
        json.put("Msg",msg);
        log.info("解冻成功？"+json.toJSONString());
        response.getWriter().print(json.toJSONString());
    }
    @RequestMapping("/deleteUser")
    private void deleteUser(HttpServletRequest request,HttpServletResponse response) throws IOException {
        boolean msg = userService.deleteUser(request.getParameter("user_id"));
        JSONObject json = new JSONObject();
        json.put("Msg",msg);
        log.info("删除成功？"+json.toJSONString());
        response.getWriter().print(json.toJSONString());
    }
}
